package com.software.car.controller;

import com.software.car.common.utils.CommonResult;
import com.software.car.service.FeaturesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 *
 * 提权管理
 *
 * @Author 魔法はまだ解けない
 * @Date 2021/3/24
 */
@RestController
@RequestMapping("/feature")
public class FeaturesController {

    @Autowired
    FeaturesService featuresService;

    /**
     * 获取提权码
     * @return
     */
    @GetMapping("/permissionCode")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER')")
    public CommonResult generatePermissionCode(){
        HashMap<String, Integer> secret = featuresService.getSecret();
        return CommonResult.success(secret);
    }

    /**
     * 提权码开启
     * @return
     */
    @PostMapping("/permissionCode/open")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER')")
    public CommonResult openPermissionCode(){
        boolean open = featuresService.open();
        return CommonResult.success(open);
    }

    /**
     * 提权码关闭
     * @return
     */
    @PostMapping("/permissionCode/close")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER')")
    public CommonResult closePermissionCode(){
        boolean open = featuresService.close();
        return CommonResult.success(open);
    }

}
